package com.agann.util;

public class EMA_Util extends Util{
	
	public EMA_Util(int n) {
		super(n);
	}
	
	
	public void update(Double num) {
		if (history.size()<n) {
			history.add(num);
		} else if (history.size() == n) {
			history.add(num);
			for (Double r : history) {
				score += r/n;
			}
		} else {
			score = num*getK() + score*(1-getK());
			history.pollFirst();
			history.add(num);
		}
	}
	
	/**
	 * Calculate k for updating EMA
	 * @return
	 */
	private double getK() {
		return 2/(1+n);
	}

}
